﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WCSY.DAL
{
    public class fg_InitialBuDetService
    {
        public fg_InitialBuDetService()
		{}
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DBHelper.GetMaxID("lineid", "fg_InitialBuDet");
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sysno, int lineid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from fg_InitialBuDet");
            strSql.Append(" where sysno='" + sysno + "' and lineid=" + lineid + " ");
            return DBHelper.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(WCSY.MODEL.fg_InitialBuDet MODEL)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (MODEL.sysno != null)
            {
                strSql1.Append("sysno,");
                strSql2.Append("'" + MODEL.sysno + "',");
            }
            if (MODEL.lineid != null)
            {
                strSql1.Append("lineid,");
                strSql2.Append("" + MODEL.lineid + ",");
            }
            if (MODEL.deptno != null)
            {
                strSql1.Append("deptno,");
                strSql2.Append("'" + MODEL.deptno + "',");
            }
            if (MODEL.deptname != null)
            {
                strSql1.Append("deptname,");
                strSql2.Append("'" + MODEL.deptname + "',");
            }
            if (MODEL.costsno != null)
            {
                strSql1.Append("costsno,");
                strSql2.Append("'" + MODEL.costsno + "',");
            }
            if (MODEL.costsname != null)
            {
                strSql1.Append("costsname,");
                strSql2.Append("'" + MODEL.costsname + "',");
            }
            if (MODEL.bumoney != null)
            {
                strSql1.Append("bumoney,");
                strSql2.Append("" + MODEL.bumoney + ",");
            }
            strSql.Append("insert into fg_InitialBuDet(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            return strSql.ToString();
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(WCSY.MODEL.fg_InitialBuDet MODEL)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update fg_InitialBuDet set ");
            if (MODEL.deptno != null)
            {
                strSql.Append("deptno='" + MODEL.deptno + "',");
            }
            else
            {
                strSql.Append("deptno= null ,");
            }
            if (MODEL.deptname != null)
            {
                strSql.Append("deptname='" + MODEL.deptname + "',");
            }
            else
            {
                strSql.Append("deptname= null ,");
            }
            if (MODEL.costsno != null)
            {
                strSql.Append("costsno='" + MODEL.costsno + "',");
            }
            else
            {
                strSql.Append("costsno= null ,");
            }
            if (MODEL.costsname != null)
            {
                strSql.Append("costsname='" + MODEL.costsname + "',");
            }
            else
            {
                strSql.Append("costsname= null ,");
            }
            if (MODEL.bumoney != null)
            {
                strSql.Append("bumoney=" + MODEL.bumoney + ",");
            }
            else
            {
                strSql.Append("bumoney= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where sysno='" + MODEL.sysno + "' and lineid=" + MODEL.lineid + " ");
            int rowsAffected = DBHelper.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sysno, int lineid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fg_InitialBuDet ");
            strSql.Append(" where sysno='" + sysno + "' and lineid=" + lineid + " ");
            int rowsAffected = DBHelper.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除所有单据数据
        /// </summary>
        public string Delete(string sysno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fg_InitialBuDet ");
            strSql.Append(" where sysno='" + sysno + "'");
            return strSql.ToString();
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WCSY.MODEL.fg_InitialBuDet GetMODEL(string sysno, int lineid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" sysno,lineid,deptno,deptname,costsno,costsname,bumoney ");
            strSql.Append(" from fg_InitialBuDet ");
            strSql.Append(" where sysno='" + sysno + "' and lineid=" + lineid + " ");
            WCSY.MODEL.fg_InitialBuDet MODEL = new WCSY.MODEL.fg_InitialBuDet();
            DataSet ds = DBHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sysno"] != null && ds.Tables[0].Rows[0]["sysno"].ToString() != "")
                {
                    MODEL.sysno = ds.Tables[0].Rows[0]["sysno"].ToString();
                }
                if (ds.Tables[0].Rows[0]["lineid"] != null && ds.Tables[0].Rows[0]["lineid"].ToString() != "")
                {
                    MODEL.lineid = int.Parse(ds.Tables[0].Rows[0]["lineid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["deptno"] != null && ds.Tables[0].Rows[0]["deptno"].ToString() != "")
                {
                    MODEL.deptno = ds.Tables[0].Rows[0]["deptno"].ToString();
                }
                if (ds.Tables[0].Rows[0]["deptname"] != null && ds.Tables[0].Rows[0]["deptname"].ToString() != "")
                {
                    MODEL.deptname = ds.Tables[0].Rows[0]["deptname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["costsno"] != null && ds.Tables[0].Rows[0]["costsno"].ToString() != "")
                {
                    MODEL.costsno = ds.Tables[0].Rows[0]["costsno"].ToString();
                }
                if (ds.Tables[0].Rows[0]["costsname"] != null && ds.Tables[0].Rows[0]["costsname"].ToString() != "")
                {
                    MODEL.costsname = ds.Tables[0].Rows[0]["costsname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["bumoney"] != null && ds.Tables[0].Rows[0]["bumoney"].ToString() != "")
                {
                    MODEL.bumoney = decimal.Parse(ds.Tables[0].Rows[0]["bumoney"].ToString());
                }
                return MODEL;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sysno,lineid,deptno,deptname,costsno,costsname,bumoney ");
            strSql.Append(" FROM fg_InitialBuDet ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sysno,lineid,deptno,deptname,costsno,costsname,bumoney ");
            strSql.Append(" FROM fg_InitialBuDet ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM fg_InitialBuDet ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DBHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.lineid desc");
            }
            strSql.Append(")AS Row, T.*  from fg_InitialBuDet T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DBHelper.Query(strSql.ToString());
        }

        /*
        */

        #endregion  Method
    }
}
